//
//  NKHomeViewController.m
//  NewKY
//
//  Created by Dee on 2016/11/12.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKHomeViewController.h"
#import <SDCycleScrollView.h>
#import "NKWeitherHomeView.h"
#import "NKHomeCollectionViewCell.h"
#import "NKWebHomeViewController.h"
#import "NKBespokeViewController.h"

@interface NKHomeViewController ()<SDCycleScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation NKHomeViewController{
    NKWeitherHomeView *_weitherView;
    SDCycleScrollView *_cycleScrollView;
    UICollectionView *_collectionView;
    
    NSArray *_collArray;
    
}



- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        
        [self loadBaiduData];
        [self loadBannerData];
        
    }
    return self;
}


#pragma mark - collectionView Delegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSDictionary *dict = _collArray[indexPath.item];
    
    //公益活动
    if(indexPath.item == 5){
        
        [self.navigationController pushViewController:[NKWebHomeViewController new] animated:YES];
        
    }else{
        
        NKBespokeViewController *vc = [NKBespokeViewController new];
        vc.ci1_id = [dict[@"cid"]intValue];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //百度天气视图
    _weitherView = [[[NSBundle mainBundle]loadNibNamed:@"NKWeitherHomeView" owner:nil options:nil]lastObject];
    [self.view addSubview:_weitherView];
    
    //轮播器
    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 70, self.view.width, 210) delegate:self placeholderImage:[UIImage imageNamed:@"Lefts_45"]];
    [self.view addSubview:_cycleScrollView];
    //轮播器属性
    _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    
    //名医通
    UIView *mingYiView = [[[NSBundle mainBundle]loadNibNamed:@"NKMingYiView" owner:nil options:nil]lastObject];
    [self.view addSubview:mingYiView];
    [mingYiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_cycleScrollView.mas_bottom);
        make.height.mas_equalTo(40);
    }];
    
    //CollectionView
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    CGFloat mingYiMargin = self.view.height - (mingYiView.height + _cycleScrollView.height + _weitherView.height+44+20+50);
    
    layout.itemSize = CGSizeMake(self.view.width/3, mingYiMargin/2);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    [collectionView registerNib:[UINib nibWithNibName:@"NKHomeCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"NKHomeCollectionViewCell"];
    
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(mingYiView.mas_bottom);
    }];
    
    _collectionView = collectionView;
    
    [self loadCollData];
    
}

#pragma mark - CollectionView DataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NKHomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NKHomeCollectionViewCell" forIndexPath:indexPath];
    
    NSDictionary *dict = _collArray[indexPath.item];
    
    cell.imageView.image = [UIImage imageNamed:dict[@"imgSrc"]];
    cell.titleLabel.text = dict[@"title"];
    
    return cell;
}

#pragma mark - 读取 Collection 数据
-(void)loadCollData{
    
    NSArray *titleArray = @[@"肿瘤",@"心血管",@"神经科",@"血液科",@"骨科",@"公益活动"];
    NSArray *cidArray = @[@"1",@"2",@"3",@"4",@"5",@"http://202.106.210.115:18080/hyde-pluto-h360/nt/appEntrance.htm"];
    NSArray *imgArray = @[@"new_1",@"new_2",@"new_3",@"new_4",@"new_5",@"new_6"];
    
    
    NSMutableArray *dictArray = [NSMutableArray array];
    [titleArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSDictionary *dict  = [[NSDictionary alloc]initWithObjectsAndKeys:obj,@"title",cidArray[idx],@"cid",imgArray[idx],@"imgSrc",nil];
        [dictArray addObject:dict];
        
    }];
    
    _collArray = [dictArray copy];
    
}

#pragma mark - 读取 Banner 数据
-(void)loadBannerData{
    
    [NetWorkingManager sendPOSTDataWithPath:@"http://iosapi.itcast.cn/doctor/banners.json.php" withParamters:@{@"page_size":@10,@"page":@1} withProgress:^(float progress) {
        
    } success:^(BOOL isSuccess, id responseObject) {
        
        [SVProgressHUD setMinimumDismissTimeInterval:0.1];
        if(isSuccess){
            NSMutableArray *imgArray = [NSMutableArray array];
            NSMutableArray *titleArray = [NSMutableArray array];
            [responseObject[@"data"][@"banners"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [imgArray addObject:obj[@"banner_img_url"]];
                [titleArray addObject:obj[@"banner_title"]];
            }];
            
            [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                
                _cycleScrollView.titlesGroup = titleArray;
                _cycleScrollView.imageURLStringsGroup = imgArray;
                
            }];
        }
        
    } failure:^(NSError *error) {
        
        [self loadBannerData];
        
    }];
    
}


#pragma mark - 读取天气数据
-(void)loadBaiduData{
    
    [NetWorkingManager requestGETDataWithPath:@"https://free-api.heweather.com/v5/now?city=%E6%B7%B1%E5%9C%B3&key=6f829bbade2b43999905f3a97fd2f9d4" withParamters:nil withProgress:^(float progress) {
        
    } success:^(BOOL isSuccess, id responseObject) {
        
        if(isSuccess){
            NSDictionary *dict = responseObject[@"HeWeather5"][0];
            
            [[NSOperationQueue mainQueue]addOperationWithBlock:^{
                _weitherView.currentCityLabel.text = dict[@"basic"][@"city"];
                
                _weitherView.weitherLabel.text = dict[@"now"][@"cond"][@"txt"];
                _weitherView.temperatureLabel.text = [NSString stringWithFormat:@"%@°",dict[@"now"][@"fl"]];
                _weitherView.dateLabel.text = dict[@"basic"][@"update"][@"loc"];
                _weitherView.fengLabel.text = dict[@"now"][@"wind"][@"dir"];
                _weitherView.fengdataLabel.text = dict[@"now"][@"wind"][@"sc"];
                
                NSString *imgSrc = [NSString stringWithFormat:@"http://files.heweather.com/cond_icon/%@.png",dict[@"now"][@"cond"][@"code"]];
                [_weitherView.imgView setImageURL:[NSURL URLWithString:imgSrc]];
            }];
        }
        
    } failure:^(NSError *error) {
        
        [self loadBaiduData];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
